package dao.impl;

import dao.IFinancialDao;
import entity.Financial;
import entity.User;
import utils.DBconn;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FinancialDaoImpl implements IFinancialDao {

    @Override
    public boolean insert(Financial financial) {
        if (financial == null) return false;

        try
        {
            // 初始化连接
            DBconn.init();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DBconn.addUpdDel(" Insert Into financial(type, username, debit_count, lender_count, count, date) VALUE(" +
                    "'"+financial.getType()+"',"+
                    "'"+financial.getUsername()+"',"+
                    +financial.getDebit_count()+","+
                    +financial.getLender_count()+","+
                    +financial.getCount()+","+
                    "str_to_date('"+sdf.format(new Date())+"', '%Y-%m-%d %H:%i:%s'))"
            );
            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean delete(String id) {
        try
        {
            // 初始化连接
            DBconn.init();
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(id);
            if(isNum.matches()) DBconn.addUpdDel("Delete from financial Where id="+id);
            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean delete(Financial financial) {
        if (financial == null) return false;
        return delete(financial.getId());
    }

    @Override
    public boolean update(Financial financial) {

        if (financial == null) return false;

        try
        {
            // 初始化连接
            DBconn.init();
            DBconn.addUpdDel(" Update financial Set " +
                    "type='"+financial.getType()+"',"+
                    "username='"+financial.getUsername()+"',"+
                    "debit_count="+financial.getDebit_count()+","+
                    "lender_count="+financial.getLender_count()+","+
                    "count="+financial.getCount()+","+
                    "date=str_to_date('"+financial.getDate()+"', '%Y-%m-%d %H:%i:%s')"+
                    " Where id=" + financial.getId()
            );

            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public List<Financial> getAllFinancialInfo(User user) {

        List<Financial> out = new LinkedList<>();

        if (user == null) return out;

        try{
            // 初始化连接
            DBconn.init();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            ResultSet res = DBconn.selectSql("select * from financial where username = '"+ user.getUsername() +"'");
            // 遍历结果集
            while(res.next())
            {
                // 创建并映射属性
                Financial financial = new Financial();
                financial.setId(String.format("%010d",res.getInt("id")));
                financial.setType(res.getString("type"));
                financial.setUsername(res.getString("username"));
                financial.setDebit_count(res.getFloat("debit_count"));
                financial.setLender_count(res.getFloat("lender_count"));
                financial.setCount(res.getFloat("count"));
                financial.setDate(sdf.format(res.getDate("date")));
                out.add(financial);
            }
            DBconn.closeConn();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return out;
    }
    public List<Financial> getAllFinancialInInfo(User user) {

        List<Financial> out = new LinkedList<>();

        if (user == null) return out;

        try{
            // 初始化连接
            DBconn.init();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            ResultSet res = DBconn.selectSql("select * from financial where username = '"+ user.getUsername() +"' and type='income'");
            // 遍历结果集
            while(res.next())
            {
                // 创建并映射属性
                Financial financial = new Financial();
                financial.setId(String.format("%010d",res.getInt("id")));
                financial.setType(res.getString("type"));
                financial.setUsername(res.getString("username"));
                financial.setDebit_count(res.getFloat("debit_count"));
                financial.setLender_count(res.getFloat("lender_count"));
                financial.setCount(res.getFloat("count"));
                financial.setDate(sdf.format(res.getDate("date")));
                out.add(financial);
            }
            DBconn.closeConn();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return out;
    }
    public List<Financial> getAllFinancialOutInfo(User user) {

        List<Financial> out = new LinkedList<>();

        if (user == null) return out;

        try{
            // 初始化连接
            DBconn.init();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            ResultSet res = DBconn.selectSql("select * from financial where username = '"+ user.getUsername() +"' and type='expend'");
            // 遍历结果集
            while(res.next())
            {
                // 创建并映射属性
                Financial financial = new Financial();
                financial.setId(String.format("%010d",res.getInt("id")));
                financial.setType(res.getString("type"));
                financial.setUsername(res.getString("username"));
                financial.setDebit_count(res.getFloat("debit_count"));
                financial.setLender_count(res.getFloat("lender_count"));
                financial.setCount(res.getFloat("count"));
                financial.setDate(sdf.format(res.getDate("date")));
                out.add(financial);
            }
            DBconn.closeConn();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return out;
    }
}
